package com.cskaoyan.wordmemorize.service.impl.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.GoodsConverter;
import com.cskaoyan.wordmemorize.dao.entity.GoodsDO;
import com.cskaoyan.wordmemorize.dao.mapper.GoodsMapper;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppGetProductServiceImpl {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsConverter goodsConverter;

    // *获取所有的商品数据(移动端)
    public PageDTO<GoodsDTO> getProductsPage(PageRequest request){
        Page<GoodsDO> goodsPage = new Page<>(request.getPageNum(), request.getCount());
        List<GoodsDO> records
                = goodsMapper.selectPage(goodsPage, null).getRecords();
        long total = goodsPage.getTotal();

        // TODO: 转换为DTO
        PageDTO<GoodsDTO> goodsDTOPageDTO
                = goodsConverter.goodsPage2PageDTO(records, total);

        return goodsDTOPageDTO;

    }
}
